package a10_动态规划;

/**
 * <p>
 * a30_打家劫舍2复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/3/12
 */
public class a30_打家劫舍2复习2 {
    //dp[i]：
    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        int rob1 = rob1(splitArray(nums, 0, nums.length - 2));
        int rob2 = rob1(splitArray(nums, 1, nums.length - 1));
        return Math.max(rob1, rob2);
    }

    private int[] splitArray(int[] nums,int leftIdx,int rightIdx){
        int[] result = new int[rightIdx-leftIdx+1];

        for (int i = 0; i < result.length; i++) {
            result[i] = nums[leftIdx];
            leftIdx++;
        }

        return result;
    }

    private int rob1(int[] nums){
        if(nums.length == 1){
            return nums[0];
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0],nums[1]);

        for (int i = 2; i < nums.length; i++) {
            dp[i] = Math.max(dp[i-1],nums[i] + dp[i-2]);
        }

        return dp[dp.length-1];
    }
}
